package com.pshcp.service;

import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.framework.util.SystemUtil;
import com.github.pagehelper.PageHelper;
import com.pshcp.common.enums.CommonStatusEnum;
import com.pshcp.domain.dto.DoctorMessageNotificationSettingsDTO;
import com.pshcp.domain.model.DoctorMessageNotificationSettings;
import com.pshcp.mapper.DoctorMessageNotificationSettingsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 医生消息通知表 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2022-11-02
 */
@Service
public class DoctorMessageNotificationSettingsService {

    @Autowired
    private DoctorMessageNotificationSettingsMapper doctorMessageNotificationSettingsMapper;

    public DoctorMessageNotificationSettings getDoctorMessageNotificationSettingsByDoctorId(Long doctorId) {
        Example example = new Example(DoctorMessageNotificationSettings.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("doctorId", doctorId);
        criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        example.setOrderByClause(" created_time desc");
        List<DoctorMessageNotificationSettings> list = doctorMessageNotificationSettingsMapper.selectByExample(example);
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

    public PageList<DoctorMessageNotificationSettings> list(DoctorMessageNotificationSettingsDTO doctorMessageNotificationSettingsDTO){
        if(doctorMessageNotificationSettingsDTO.getPageNum() != null && doctorMessageNotificationSettingsDTO.getPageSize() != null){
            PageHelper.startPage(doctorMessageNotificationSettingsDTO.getPageNum(), doctorMessageNotificationSettingsDTO.getPageSize());
        }
        Example example = new Example(DoctorMessageNotificationSettings.class);
        Example.Criteria criteria = example.createCriteria();
        if(doctorMessageNotificationSettingsDTO.getStatus() != null){
            criteria.andEqualTo("status", doctorMessageNotificationSettingsDTO.getStatus());
        }
        example.setOrderByClause(" created_time desc");
        List<DoctorMessageNotificationSettings> list = doctorMessageNotificationSettingsMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<DoctorMessageNotificationSettings> select(DoctorMessageNotificationSettings doctorMessageNotificationSettings){
        return doctorMessageNotificationSettingsMapper.select(doctorMessageNotificationSettings);
    }

    public int insert(DoctorMessageNotificationSettings doctorMessageNotificationSettings){
        doctorMessageNotificationSettings.setId(SystemUtil.getId());
        doctorMessageNotificationSettings.setStatus(CommonStatusEnum.INIT.getCode());
        doctorMessageNotificationSettings.setCreatedTime(new Date());
        return doctorMessageNotificationSettingsMapper.insert(doctorMessageNotificationSettings);
    }

    public int update(DoctorMessageNotificationSettings doctorMessageNotificationSettings){
        doctorMessageNotificationSettings.setUpdateTime(new Date());
        return doctorMessageNotificationSettingsMapper.updateByPrimaryKeySelective(doctorMessageNotificationSettings);
    }

    public DoctorMessageNotificationSettings findById(Long id){
        return doctorMessageNotificationSettingsMapper.selectByPrimaryKey(id);
    }

    public List<DoctorMessageNotificationSettings> findByIds(List<Long> ids){
        Example example = new Example(DoctorMessageNotificationSettings.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return doctorMessageNotificationSettingsMapper.selectByExample(example);
    }

    public int delById(Long id){
        Example example = new Example(DoctorMessageNotificationSettings.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        DoctorMessageNotificationSettings doctorMessageNotificationSettings = new DoctorMessageNotificationSettings();
        doctorMessageNotificationSettings.setStatus(CommonStatusEnum.DEL.getCode());
        return doctorMessageNotificationSettingsMapper.updateByExampleSelective(doctorMessageNotificationSettings, example);
    }

}
